package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.evenbus.PFCaiSelectNumMessage;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class ProcurementTypeListItemAdapter extends RecyclerView.Adapter<ProcurementTypeListItemAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ProcurementTypeListBeen.DataBean.GoodsListBean> list;
    private Context context;
    private ProcurementTypeListAdapter adapter;
    private int yiji_position;


    public ProcurementTypeListItemAdapter(Context context, List<ProcurementTypeListBeen.DataBean.GoodsListBean> list, ProcurementTypeListAdapter adapter, int yiji_position) {
        this.list = list;
        this.context = context;
        this.adapter = adapter;
        this.yiji_position = yiji_position;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_procurement_type_list_item, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        if (list.get(position).getPurchase_type() == 1) {
            holder.tvPinzhong.setText(list.get(position).getGoods_name());
            holder.tvPrice.setText(list.get(position).getGoods_min_price() + "～" + list.get(position).getGoods_max_price());
            holder.tvPinzhong.setVisibility(View.VISIBLE);
            holder.etPinzhong.setVisibility(View.GONE);
            holder.tvPrice.setVisibility(View.VISIBLE);
            holder.etPrice.setVisibility(View.GONE);
        } else {
            holder.tvPinzhong.setText("");
            holder.tvPinzhong.setVisibility(View.GONE);
            holder.etPinzhong.setVisibility(View.VISIBLE);
            holder.tvPrice.setText("");
            holder.tvPrice.setVisibility(View.GONE);
            holder.etPrice.setVisibility(View.VISIBLE);
        }

        holder.tvGuige.setText(list.get(position).getGoods_spec());

        holder.tvChandi.setText(list.get(position).getGoods_producer());

        if (list.get(position).getGoods_name().isEmpty()) {
            holder.etPinzhong.setText("");
        } else {
            holder.etPinzhong.setText(list.get(position).getGoods_name() + "");
        }
        if (list.get(position).getPurchase_type() == 2) {
            if (list.get(position).getGoods_min_price().isEmpty() && list.get(position).getGoods_max_price().isEmpty()) {
                holder.etPrice.setText("");
            } else {
                holder.etPrice.setText(list.get(position).getGoods_max_price()+ "");
            }
        }
        if (list.get(position).getGoods_num() == 0) {
            holder.etXuqiu.setText("");
        } else {
            holder.etXuqiu.setText(list.get(position).getGoods_num() + "");
        }
        if (list.get(position).isIs_select()) {
            holder.ivSelect.setImageResource(R.drawable.icon_cai_select_yes);
            holder.viewBg.setVisibility(View.VISIBLE);
        } else {
            holder.ivSelect.setImageResource(R.drawable.icon_cai_select_no);
            holder.viewBg.setVisibility(View.GONE);
        }

        holder.ivSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setIs_select(!list.get(position).isIs_select());
                adapter.notifyDataSetChanged();
                notifyDataSetChanged();
                EventBus.getDefault().post(new PFCaiSelectNumMessage(0));
            }
        });

        PinZhongETWatcher pinZhongETWatcher = new PinZhongETWatcher(position);
        holder.etPinzhong.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText mV = (EditText) v;
                if (hasFocus) {
                    mV.addTextChangedListener(pinZhongETWatcher);
                } else {
                    mV.removeTextChangedListener(pinZhongETWatcher);
                }
            }
        });

        PriceETWatcher priceETWatcher = new PriceETWatcher(position);
        holder.etPrice.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText mV = (EditText) v;
                if (hasFocus) {
                    mV.addTextChangedListener(priceETWatcher);
                } else {
                    mV.removeTextChangedListener(priceETWatcher);
                }
            }
        });

        GuanJianCiZuETWatcher watcher = new GuanJianCiZuETWatcher(position);
        holder.etXuqiu.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText mV = (EditText) v;
                if (hasFocus) {
                    mV.addTextChangedListener(watcher);
                } else {
                    mV.removeTextChangedListener(watcher);
                }
            }
        });

        if (list.get(position).isIs_jiaodian()) {
            if (list.get(position).getJiaodian_type() == 1) {
                holder.etPinzhong.setFocusable(true);
                holder.etPinzhong.setFocusableInTouchMode(true);
                holder.etPinzhong.requestFocus();
                holder.etPinzhong.setSelection(holder.etPinzhong.getText().length());
            } else if (list.get(position).getJiaodian_type() == 2) {
                holder.etPrice.setFocusable(true);
                holder.etPrice.setFocusableInTouchMode(true);
                holder.etPrice.requestFocus();
                holder.etPrice.setSelection(holder.etPrice.getText().length());
            } else if (list.get(position).getJiaodian_type() == 3) {
                holder.etXuqiu.setFocusable(true);
                holder.etXuqiu.setFocusableInTouchMode(true);
                holder.etXuqiu.requestFocus();
                holder.etXuqiu.setSelection(holder.etXuqiu.getText().length());
            }
            list.get(position).setIs_jiaodian(false);

        }

    }

    class PinZhongETWatcher implements TextWatcher {

        int position;

        public PinZhongETWatcher(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().isEmpty()) {
                list.get(position).setGoods_name("");
            } else {
                list.get(position).setGoods_name(s.toString());
            }
            list.get(position).setIs_jiaodian(true);
            list.get(position).setJiaodian_type(1);
            adapter.notifyItemChanged(yiji_position);
            EventBus.getDefault().post(new PFCaiSelectNumMessage(0));
        }
    }

    class PriceETWatcher implements TextWatcher {

        int position;

        public PriceETWatcher(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().isEmpty()) {
                list.get(position).setGoods_min_price("");
                list.get(position).setGoods_max_price("");
            } else {
                list.get(position).setGoods_min_price(s.toString());
                list.get(position).setGoods_max_price(s.toString());
            }
            list.get(position).setIs_jiaodian(true);
            list.get(position).setJiaodian_type(2);
            adapter.notifyItemChanged(yiji_position);
            EventBus.getDefault().post(new PFCaiSelectNumMessage(0));
        }
    }

    class GuanJianCiZuETWatcher implements TextWatcher {

        int position;

        public GuanJianCiZuETWatcher(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().isEmpty()) {
                list.get(position).setGoods_num(0);
            } else {
                list.get(position).setGoods_num(Integer.parseInt(s.toString()));
            }
            list.get(position).setIs_jiaodian(true);
            list.get(position).setJiaodian_type(3);
            adapter.notifyItemChanged(yiji_position);
            EventBus.getDefault().post(new PFCaiSelectNumMessage(0));
        }
    }


    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.view_bg)
        View viewBg;
        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.tv_pinzhong)
        TextView tvPinzhong;
        @BindView(R.id.et_pinzhong)
        EditText etPinzhong;
        @BindView(R.id.et_price)
        EditText etPrice;
        @BindView(R.id.tv_guige)
        TextView tvGuige;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.et_xuqiu)
        EditText etXuqiu;
        @BindView(R.id.tv_chandi)
        TextView tvChandi;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
